/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索:">
        <el-input
          size="small"
          v-model="formInline.carGoods.goodsName"
          placeholder="输入商品名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="选择商品类型" >
          <el-select v-model="formInline.carGoods.goodsStyle" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="id" label="商品id" width="80" align="center">
      </el-table-column>
      <el-table-column sortable prop="goodsName" label="商品名字" width="120">
      </el-table-column>
      <el-table-column sortable prop="price" label="价格" width="100">
      </el-table-column>
      <el-table-column sortable prop="sellPrice" label="售价" width="100">
      </el-table-column>

      <el-table-column align="center" label="商品图片" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="handimg(scope.$index, scope.row)" >查看</el-button>
        </template>
      </el-table-column>

      <el-table-column sortable prop="sepcification" label="商品型号" width="200">
      </el-table-column>
      <el-table-column sortable prop="recommendChinese" label="是否推荐" width="100">
      </el-table-column>
      <el-table-column sortable prop="storeChinese" label="上架门店" width="150">
      </el-table-column>
      <el-table-column sortable prop="styleChinese" label="商品类型" width="100">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteUser(scope.$index, scope.row)">删除</el-button>

            <el-button
            size="mini"
            v-if="scope.row.recommend === 0"
            @click="recommend(scope.$index, scope.row)">推荐</el-button>


            <el-button
            size="mini"
            type="danger"
            v-if="scope.row.recommend === 1"
            @click="disRecommend(scope.$index, scope.row)">取消推荐</el-button>


        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑/增加界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="30%"
      @click="closeDialog"
    >
      <el-form label-width="120px" :model="userFrom" ref="userFrom">
        <el-form-item label="商品名字" prop="goodsName">
          <el-input
            size="small"
            v-model="userFrom.goodsName"
            auto-complete="off"
            placeholder="请输入商品名字"
          ></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input
            size="small"
            v-model="userFrom.price"
            auto-complete="off"
            placeholder="请输入价格"
          ></el-input>
        </el-form-item>
        <el-form-item label="售价" prop="sellPrice">
          <el-input
            size="small"
            v-model="userFrom.sellPrice"
            auto-complete="off"
            placeholder="售价"
          ></el-input>
        </el-form-item>



        <el-form-item label="选择商品照片" prop="img">
                      <el-upload
            class="upload-demo"
            ref="upload"
            action="http://localhost:9000/ses/mdStore/upload"
            :file-list="fileList"
            :auto-upload="false"
            :on-change="changeFile"
            :on-success="handleUploadSuccess"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button
            >
            <el-button
              style="margin-left: 10px"
              size="small"
              type="success"
              @click="submitUpload"
              >上传到服务器</el-button
            >
          </el-upload>
        </el-form-item>



        <el-form-item label="商品型号" prop="sepcification">
          <el-input
            size="small"
            v-model="userFrom.sepcification"
            auto-complete="off"
            placeholder="请输入商品型号"
          ></el-input>
        </el-form-item>



        <el-form-item label="选择是否推荐" >
          <el-select v-model="userFrom.recommend" placeholder="请选择">
            <el-option
              v-for="item in recommends"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="选择上架门店" >
          <el-select v-model="userFrom.storeId" placeholder="请选择">
            <el-option
              v-for="item in stores"
              :key="item.storeId"
              :label="item.storeName"
              :value="item.storeId">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="选择商品类型" >
          <el-select v-model="userFrom.goodsStyle" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>



        <!--角色选择下拉框-->
        <!--
        <el-form-item label="选择角色" >
          <el-select v-model="userFrom.role_id" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('userFrom')"
          >保存</el-button
        >
      </div>
    </el-dialog>

    <!--查看图片-->
    <el-dialog
      title="图片"
      :visible.sync="centerDialogVisible"
      width="33%" center>
      <img width="80%" :src="userFrom.img" alt="">

      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "../../components/Pagination";
import * as shopping from "../../api/shoppingManage";
import axios from "axios";
export default {
  //model
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      centerDialogVisible: false,//控制图片显示页面的显示与隐藏
      title: "添加",
      userFrom: {
        id: "",
        goodsName: "",
        goodsStyle: "",
        img: "",
        price: "",
        sellPrice: "",
        recommend: "",
        recommendChinese: "",
        sepcification: "",
        storeChinese: "",
        storeId: "",
        styleChinese: ""
      },

      formInline: {
        page: 1,
        limit: 5,
        carGoods: {
          goodsName: "",
          goodsStyle: "",
          recommend: "",
        },
      },
      // 删除部门
      seletedata: {
        ids: "",
        token: localStorage.getItem("logintoken"),
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 5,
        total: 10,
      },

      //下拉框属性
      options: [],
      stores:[],
      recommends:[],
      fileList:[],
      value: "",
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getDate();
    this.getGoodsStyle();
    this.getStore();
    this.getRecommend();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    getGoodsStyle(){
      shopping.selectGoodsStyle().then((res) =>{
        if(res.data.code == 200){
          this.options = res.data.data;
        }
      })
    },
    getStore(){
      shopping.selctStore().then((res) =>{
        if(res.data.code ==200){
          this.stores = res.data.data;
        }
      })
    },


    submitUpload(){//上传文件
        this.$refs.upload.submit();
    },
    changeFile(file,fileList){
       this.fileList = [];
       this.fileList.push(fileList[fileList.length-1]);
    },
    handleUploadSuccess(response){
        if(response && response.code == 200){
            this.userFrom.img = response.data;
        }
    },




    getRecommend(){
      shopping.selectRecommend().then((res)=>{
        if(res.data.code == 200){
          this.recommends = res.data.data;
        }
      })
    },

    getDate() {
      shopping
        .selectGoods(this.formInline.page, this.formInline.limit)
        .then((res) => {
          if (res.data.code == 200) {
            this.listData = res.data.data.list;
            this.pageparm.total = res.data.data.total;
            console.log(this.listData);
          }
        });

    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.search();
    },
    // 搜索事件
    search() {
      shopping.selectMH(this.formInline).then((res)=>{
        if(res.data.code == 200){
          
          this.listData = res.data.data.list;
          this.pageparm.total = res.data.data.total;

        }
      })
    },
    //显示图片
    handimg: function(index, row){
      this.centerDialogVisible = true;
      this.userFrom.img = row.img;
    },


    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改";
        this.userFrom.id = row.id;
        this.userFrom.goodsName = row.goodsName;
        this.userFrom.price = row.price;
        this.userFrom.sellPrice = row.sellPrice;
        this.userFrom.img = row.img;
        this.userFrom.sepcification = row.sepcification;
        this.userFrom.recommend = row.recommend;
        this.userFrom.storeId = row.storeId;
        this.userFrom.goodsStyle = row.goodsStyle;
      } else {
        this.title = "添加";
        this.userFrom.goodsName = "";
        this.userFrom.price = "";
        this.userFrom.sellPrice = "";
        this.userFrom.img = "";
        this.userFrom.sepcification = "";
        this.userFrom.recommend = "";
        this.userFrom.storeId = "";
        this.userFrom.goodsStyle = "";
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          //下面这个是 系统封装好的接口方法!
          // deptSave(this.userFrom)
          if (this.title == "添加") {
              shopping.inserGoods(this.userFrom).then((res)=>{
                this.editFormVisible = false;
                this.loading = false; //加载状态
                if(res.data.code == 200){
                  this.$message({
                    message:"新增商品成功",
                    type:"success",
                  })
                }else{
                  this.$message({
                    message:"新增商品失败",
                    type:"error",
                  })
                }
                this.getDate();
              })
              
          } else if (this.title == "修改") {

            shopping.updateGoods(this.userFrom).then((res)=>{
              this.editFormVisible = false;
              this.loading = false; //加载状态
              if(res.data.code == 200){
                this.$message({
                  message:'修改商品成功',
                  type:'success'
                })
              }else{
                this.$message({
                  message:'修改商品失败',
                  type:'error'
                })
              }
              this.getDate();
            })
          }
        }
      });
    },
    // 删除商品
    deleteUser(index, row) {
      this.$confirm("确定要删除此条商品吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          shopping.deleteGoods(row.id).then((res) => {
            if(res.data.code == 200){
                this.$message({
                  message:'删除商品成功',
                  type:'success'
                })
                this.getDate();
            }else{
              this.$message({
                message:'删除商品失败',
                type: 'error'
              })
            }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      })
    },

    //推荐
    recommend(index , row){

      this.$confirm("确定要推荐此商品吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          shopping.recommend(row.id).then((res) =>{
            if(res.data.code==200){
              this.$message({
                message:'推荐成功',
                type: 'success'
              })
            }
            this.getDate();
          })

        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
          });
        });

    },


    //取消推荐
    disRecommend(index , row){
        this.$confirm("确定要取消推荐此商品吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          shopping.disRecommend(row.id).then((res) =>{
            if(res.data.code == 200){
              this.$message({
                message:"已成功取消推荐",
                type:'success'
              })
            }
            this.getDate();
          })

        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
          });
        });
    },

    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 